/**
 一个正整数的阶乘（factorial）
 是所有小于及等于该数的正整数的积，
 并且0的阶乘为1。自然数n的阶乘写作n!

 */

function factorialize(num) {
    var n = num;
    var result=1;
    if (num < 0) {
        return -1;
    }
    else if (num <= 1) {
        return 1;
    }
    else {
        for(var i=1;i<=num;i++){
            result *= i
        }
    }
    return result
}

console.log(factorialize(7))



    //递归,写法一：
    function factorial(num,sum=1) {
        if (num < 0) {
            return -1;
        }
        if (num <=1) {return sum;}
        else{
            return factorial(num - 1, num * sum);
        }

    }
    console.log(factorial(7));







//递归,写法二：
    function factorial(num) {
        if (num < 0) {
            return -1;
        } else if (num <= 1) {
            return 1;
        } else {
            return (num * factorial(num - 1));
        }
    }
    console.log(factorial(7));


